<!-- HTML FOR TESTING THE POST COMMAND -->
<html>
	<form action="newsong.php" method="post">
	UserId: <input type="text" name="id" />
	Artist: <input type="text" name="artist" />
	Title: <input type="text" name="title" />
	<input type="submit" />
	</form>
</html>

<?php

	function checkResult($result,$query){
		if(!$result){
			$message  = 'Invalid query: ' . mysql_error() . "\n";
			$message .= 'Whole query: ' . $query;
			die($message);
		}	
	}


	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	
		mysql_connect("localhost","akosciak","MojaPtaszek");
		
		mysql_select_db("songbump") or die( "Unable to select database");

		$id = $_POST["id"];
		$ip = $_POST["ip"];
		$artist = $_POST["artist"];
		$title = $_POST["title"];

		$query = "SELECT * FROM Users WHERE Id='".$id."'";
		$result = mysql_query($query);
		checkResult($result,$query);
		if (mysql_num_rows($result) != 1){
			die();
		}
	
		$query = "UPDATE Users SET CurrentlyPlaying='1' ip='".$ip." WHERE Id='".$id."'";
		$result = mysql_query($query);
		checkResult($result,$query);

		$values = "('".$artist."','".$title."')";
		$query = "INSERT INTO `Songs`(`Artist`, `Title`) VALUES ".$values;
		$result = mysql_query($query);
		checkResult($result,$query);

		$values = "(".$id.",'".mysql_insert_id()."')";
		$query = "INSERT INTO `SongsPlayed`(`User`,`Song`) VALUES ".$values;
		$result = mysql_query($query);
		checkResult($result,$query);
	}
	
?>
